<?php
/*
 * 文件上传（fckeditor的上传），下载
 */
require './include/common.inc.php';
require 'attachment.class.php';
$action = @get_null($action);
$site_id = get_current_site();

switch($action)
{
    case 'upload':
		if($dosubmit)
	    {
            $site_url = site_url($siteid);
			$attachment = new attachment();
            $attachment->siteid = @get_null($siteid);
			$aids = $attachment->upload('uploadfile');
            if(empty($aids))
            {
                if($from == 'fckeditor')
                {
                    $message = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8".CHARSET."\"><script language='javascript'>";
					$message .= "alert('".$attachment->error()."');";
					$message .= "</script>";
					echo $message;exit;
                }
                else
                {
                    showmessage($attachment->error());
                }
            }
			$filename = $attachment->uploadedfiles[0]['filename'];
			$filepath = $attachment->uploadedfiles[0]['filepath'];
			$fileurl = $site_url.$filepath;
			$extension = fileext($filename);
			
			if($from == 'fckeditor')
			{
				$fileurl = $fileurl;
				if(empty($filepath))
				{
					$message = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8".CHARSET."\"><script language='javascript'>";
					$message .= "alert('".$attachment->error()."');";
					$message .= "</script>";
					echo $message;exit;
				}
				$filename = basename($filepath);
				if(isset($id))
				{
					$message = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8".CHARSET."\"><script language='javascript'>";
					$message .= "window.parent.show_ok('0-".$id."','".md5($fileurl)." $MM_objid $fileurl');";
					$message .= "</script>";
				}
				else
				{
					$message = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8".CHARSET."\"><script language='javascript'>";
					$message .= "window.parent.SetUrl('$fileurl', '', '', '$filename');";
					$message .= "</script>";
				}
			}
			exit($message);
		}
		
		break;
    
    case 'upload_thumb' :

        $attachment = new attachment();
        $attachment->siteid = @get_null(get_current_site());
        
        if(isset($dosubmit))
        {
            $maxwidth = isset($maxwidth) ? intval($maxwidth) : 350;
            $maxheight = isset($maxheight) ? intval($maxheight) : 350;
            
            $aids = $attachment->upload('thumb', ALLOW_THUMB_EXT, ALLOW_THUMB_SIZE);
            if(empty($aids))
            {
                showmessage($attachment->error(),'goback');
            }
            else
            {
                $file = $attachment->get_one($aids);
                $site_root = site_root($file['siteid']);//获取站点相对地址
                $thumb = thumb($site_root.$file['filepath'], $maxwidth, $maxheight, 0);
                $thumb = site_url(get_current_site()). $file['filepath'];
                include template('admin', 'upload_thumb');
            }
        }
        else
        {
            $thumb = 'css/images/nopic.gif';
            $maxwidth = isset($maxwidth) ? intval($maxwidth) : 350;
            $maxheight = isset($maxheight) ? intval($maxheight) : 350;
            $size = $attachment->size(ALLOW_THUMB_SIZE);
            include template('admin', 'upload_thumb');
        }
        break;

    case 'upload_file' :

        $site_url = site_url($site_id);
        $attachment = new attachment();
        $attachment->siteid = @get_null($site_id);

        if(isset($dosubmit))
        {
            $maxwidth = isset($maxwidth) ? intval($maxwidth) : 350;
            $maxheight = isset($maxheight) ? intval($maxheight) : 350;

            $aids = $attachment->upload('thumb', UPLOAD_ALLOWEXT, UPLOAD_MAXSIZE);
            if(empty($aids))
            {
                showmessage($attachment->error(),'goback');
            }
            else
            {
                $file = $attachment->get_one($aids);
                $thumb = $site_url.$file['filepath'];
                include template('admin', 'upload_thumb');
            }
        }
        else
        {
            $thumb = 'css/images/nopic.gif';
            $size = $attachment->size(UPLOAD_MAXSIZE);
            include template('admin', 'upload_thumb');
        }
        break;

}
?>
